package com.system.tools;

import java.io.File;
import java.io.FileInputStream;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class DocFileTool {
	
    public static String getWordContent(File f) throws Exception {     
        FileInputStream fis = new FileInputStream(f);     
        HWPFDocument doc = new HWPFDocument(fis);     
        Range rang = doc.getRange();     
        String text = rang.text();     
        fis.close();     
        return text;    
    }
    
	public static String getExcelContent(File f) throws Exception {
		// 构建Workbook对象, 只读Workbook对象
		// 直接从本地文件创建Workbook
		// 从输入流创建Workbook
		FileInputStream fis = new FileInputStream(f);
		StringBuilder sb = new StringBuilder();
		jxl.Workbook rwb = Workbook.getWorkbook(fis);
		// 一旦创建了Workbook，我们就可以通过它来访问
		// Excel Sheet的数组集合(术语：工作表)，
		// 也可以调用getsheet方法获取指定的工资表
		Sheet[] sheet = rwb.getSheets();
		for (int i = 0; i < sheet.length; i++) {
			Sheet rs = rwb.getSheet(i);
			for (int j = 0; j < rs.getRows(); j++) {
				Cell[] cells = rs.getRow(j);
				for (int k = 0; k < cells.length; k++)
					sb.append(cells[k].getContents());
			}
		}
		fis.close();
		return sb.toString();
	}
	
	public static String getPDFContent(File f) throws Exception {
		FileInputStream fis = new FileInputStream(f);
		PDFParser p = new PDFParser(fis);
		p.parse();
		PDDocument pdd = p.getPDDocument();
		PDFTextStripper ts = new PDFTextStripper();
		String c = ts.getText(pdd);
		pdd.close();
		fis.close();
		return c;
	}
	
	public static void main(String[] args) throws Exception {
		
	}
}
